package com.leeyip.mvvm.base

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import android.view.Window
import android.view.inputmethod.InputMethodManager
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.gyf.immersionbar.ImmersionBar
import kotlinx.android.synthetic.main.toolbar_activity.*


/**
 * Activity 基类
 */
abstract class BaseActivity : AppCompatActivity() {


    /** 状态栏沉浸  */
    private var mImmersionBar: ImmersionBar? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        getLayoutId()?.let {
            setContentView(it)
            initSoftKeyboard()
        }
        initParam(intent)
        initImmersion()
        initView()
        initToolbar()
        if (savedInstanceState == null) initData()
    }

    @LayoutRes
    abstract fun getLayoutId(): Int?

    protected abstract fun initData()

    protected abstract fun initView()

    protected fun initParam(intent: Intent) {}

    /**
     * 初始化软键盘
     */
    fun initSoftKeyboard() {
        // 点击外部隐藏软键盘，提升用户体验
        getContentView().setOnClickListener { hideSoftKeyboard() }
    }

    /**
     * 隐藏软键盘
     */
    private fun hideSoftKeyboard() {
        // 隐藏软键盘，避免软键盘引发的内存泄露
        val view = currentFocus
        view?.let {
            val manager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            if (manager.isActive(view)) {
                manager.hideSoftInputFromWindow(
                    it.windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS
                )
            }
        }
    }


    /**
     * 和 setContentView 对应的方法
     */
    fun getContentView(): ViewGroup {
        return findViewById(Window.ID_ANDROID_CONTENT)
    }

    fun initToolbar() {
        ivBack?.setOnClickListener {
            if (onBackClick()) {
                finish()
            }
        }

        ivRight?.setOnClickListener {
            onRightIconClick()
        }

        ivClose?.setOnClickListener { finish() }
    }

    /**
     * 初始化沉浸式
     */
    protected fun initImmersion() {
        //初始化沉浸式状态栏
        if (isStatusBarEnabled()) {
            getStatusBarConfig().init()
        }
    }

    /**
     * 是否使用沉浸式状态栏
     */
    protected fun isStatusBarEnabled(): Boolean {
        return true
    }

    /**
     * 状态栏字体深色模式
     */
    protected fun isStatusBarDarkFont(): Boolean {
        return true
    }

    /**
     * 初始化沉浸式状态栏
     */
    protected fun createStatusBarConfig(): ImmersionBar {
        return ImmersionBar.with(this)
            // 默认状态栏字体颜色为黑色
            .statusBarDarkFont(isStatusBarDarkFont())
    }

    /**
     * 获取状态栏沉浸的配置对象
     */
    fun getStatusBarConfig(): ImmersionBar {
        if (mImmersionBar == null) {
            mImmersionBar = createStatusBarConfig()
        }
        return mImmersionBar as ImmersionBar
    }


    open fun onBackClick(): Boolean {
        // 返回额外的工作
        return true
    }

    open fun onRightIconClick() {
        // Toolbar右上角按钮
    }

    fun setRightIcon(iconRes: Int) {
        ivRight?.let {
            it.isVisible = true
            it.setImageResource(iconRes)
        }
    }

    fun setCenterText(text: String) {
        tvCenter?.text = text
    }

    fun setCenterText(text: Int) {
        tvCenter?.setText(text)
    }

    fun setCenterTextColor(color: Int) {
        tvCenter?.setTextColor(color)
    }

    fun showCloseIcon() {
        ivClose?.isVisible = true
    }

}